package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.MerchantBiz;
import com.coolbi.onsupai.core.entity.Merchant;
import com.coolbi.security.web.action.SessionAction;

/**
 * @Description Merchant 供应商 Action
 * @author for2cold
 * @codeDate 2012-7-6 下午07:30:05
 *
 */
public class MerchantAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private Merchant merchant;
	private List<Merchant> merchantList;
	@Autowired
	private MerchantBiz merchantBiz;
	
	/**
	 * 添加供应商信息
	 * @return
	 */
	public String save() {
		try {
			merchantBiz.save(merchant);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 修改供应商信息
	 * @return
	 */
	public String update() {
		try {
			merchantBiz.update(merchant);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 获取所有供应商信息
	 * @return
	 * @throws Exception
	 */
	public String listByPage() throws Exception {
		pager.setRecordCount(merchantBiz.count());
		merchantList = merchantBiz.listByPage((pager.getCurrPageNo() - 1) * pager.getPageSize(),
					pager.getPageSize());
		return SUCCESS;
	}
	
	/**
	 * 根据ID查询供应商信息
	 * @return
	 * @throws Exception
	 */
	public String findByMerchantId() throws Exception {
		merchant = merchantBiz.get(merchant.getId());
		return SUCCESS;
	}
	
	/**
	 * 检测供应商编号是否已存在
	 * @return
	 */
	public String checkMerchantNo() {
		try {
			boolean flag = false;
			if (null == merchant.getId()) {
				flag = merchantBiz.checkMerchantNo(merchant.getMerchantNo());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 检测联系电话是否已存在
	 * @return
	 */
	public String checkTelephone() {
		try {
			boolean flag = false;
			if (null == merchant.getId()) {
				flag = merchantBiz.checkTelephone(merchant.getTelephone());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 检测供应商编号是否已存在
	 * @return
	 */
	public String checkEmail() {
		try {
			boolean flag = false;
			if (null == merchant.getId()) {
				flag = merchantBiz.checkEmail(merchant.getEmail());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	public Merchant getMerchant() {
		return merchant;
	}
	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}
	public List<Merchant> getMerchantList() {
		return merchantList;
	}
	public void setMerchantList(List<Merchant> merchantList) {
		this.merchantList = merchantList;
	}
	public void setMerchantBiz(MerchantBiz merchantBiz) {
		this.merchantBiz = merchantBiz;
	}
	
}
